#version 460

layout(triangles) in;
layout(triangle_strip, max_vertices=32) out;




#include "../../lib/project_matrix.glsl"
#include "../../lib/single_color.glsl"

layout(std430, column_major, binding = 3) readonly buffer U1_Matrix {
	float m_Scale[];
};

//in gl_PerVertex {
//	vec4	gl_Position;
//	float	gl_PointSize;
//} gl_in[];

layout(location = 1) in vec3 inPos[];
layout(location = 2) in uint inInstance[];



out gl_PerVertex {
	vec4	gl_Position;
};
layout(location = 0) out vec4 outColor;
layout(location = 1) out vec3 outNormal;
layout(location = 2) out vec3 outViewLine;

//vec3 vs[3] = {vec3(-100,-500,0), vec3(-100,500,0), vec3(500,00,0)};
void main(void) {
	
	vec3 edgeA = normalize(inPos[1] - inPos[0]);
	vec3 edgeB = normalize(inPos[2] - inPos[0]);
	vec3 Normal = cross(edgeA, edgeB);

	vec4 color = f_buildColor(InstanceColor[inInstance[0]]);

	for(int i=0; i<3; ++i){
		mat4 mat = mat_Model[inInstance[0]];

		if(m_Scale.lenght()){
			
		}

		gl_Position = mat_ProjecView[0] * mat * vec4(inPos[i], 1);
		//gl_Position = gl_in[i].gl_Position;
		mat3x3 normalMat = mat3x3(mat);
		outNormal = normalize((normalMat * Normal).xyz);

		outColor = color;
		
		outViewLine = (inverse(mat3(mat_ProjecView[1])) * vec3(0,0,1));
		//outViewLine = ((mat_ProjecView[1]) * vec4(0,0,-1,1)).xyz;
		EmitVertex();
	}

	EndPrimitive();
}


